package com.atguigu.nio.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class NewIOServer {
    public static void main(String[] args) throws IOException {
        InetSocketAddress inetSocketAddress = new InetSocketAddress(7001);

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        ServerSocket serverSocket = serverSocketChannel.socket();

        serverSocket.bind(inetSocketAddress);

        //创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);

        //多个客户端连接
        while (true){
            SocketChannel socketChannel = serverSocketChannel.accept();

            int readCount = 0;
            //一个客户端，多次读取
            while (-1 != readCount){
                try {
                    readCount = socketChannel.read(byteBuffer);
                    byteBuffer.rewind();//倒带position=0，mark作废
                }catch (Exception e){

                }
            }
        }
    }
}
